package com.passionfruit.module.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.passionfruit.module.entity.SysRoleMenu;
import com.passionfruit.module.entity.SysUserRole;
import com.passionfruit.module.mapper.SysUserRoleMapper;
import com.passionfruit.module.service.ISysRoleMenuService;
import com.passionfruit.module.service.ISysUserRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户与角色对应关系 服务实现类
 * </p>
 *
 * @author weirdor
 * @since 2017-10-31
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @Override
    public List<Long> queryRoleIdList(Long userId) {
        List<Long> longList=new ArrayList<>();
        EntityWrapper<SysUserRole> ew=new EntityWrapper<>();
        ew.eq("user_id",userId);
       List<SysUserRole> sysUserRoles=selectList(ew);
        sysUserRoles.forEach(k->{
                longList.add(k.getRoleId());
        });
        return longList;
    }

    @Override
    public void saveOrUpdate(Long userId, List<Long> roleIdList) {
        if(roleIdList.size() == 0){
            return ;
        }
        //先删除用户与角色关系
        EntityWrapper<SysUserRole> ew=new EntityWrapper<>();
        ew.eq("user_id",userId);
        super.delete(ew);
        roleIdList.forEach(k->{
            SysUserRole sysUserRole=new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleId(k.longValue());
            super.insert(sysUserRole);
        });
    }

    @Override
    public void deUserRole(Long userId) {
        //先删除用户与角色关系
        EntityWrapper<SysUserRole> ew=new EntityWrapper<>();
        ew.eq("user_id",userId);
        super.delete(ew);
    }
}
